<div class="filter" style='width:900px;'>
<div style='width:500px;float:left;'>
	<?php echo $filter ?>
</div>
<div style='width:400px;float:left'>
	<table class="boxlist2">
		<?php if (isset(Yii::app()->session['chooseOriginal'])&&!empty(Yii::app()->session['chooseOriginal'])): ?>
			<thead >
				<tr >
					<th >
						H
					</th>
					<th >
						<?php echo Yii::app()->session['chooseOriginal']['name'] ?>
					</th>
					<th >
						<?php echo Yii::app()->session['chooseOriginal']['album'] ?>
					</th>
					<th >
						<?php
							$label = '<button class="bt2"> &times; </button>';
							$rurl= array('clearOriginal');
							$htmlOption = array();
							echo CHtml::link($label,$rurl,$htmlOption);
						?>
					</th>
				</tr>
			</thead>
		<?php endif ?>
		<tbody >
		<?php $translators = Yii::app()->session['chooseTranslator']; ?>
		<?php if (!empty($translators) && is_array($translators)): ?>
		<?php foreach ($translators as $translate ): ?>
			<tr >
				<td >
					T
				</td>
				<td >
					<?php echo $translate['name'] ?>
				</td>
				<td >
					<?php echo $translate['album'] ?>
				</td>
				<td >
					<?php
						$label = '<button class="bt2"> &times; </button>';
						$rurl= array('clearTranslator','id'=>$translate['id']);
						$htmlOption = array();
						echo CHtml::link($label,$rurl,$htmlOption);
					?>
				</td>
			</tr>
		<?php endforeach ?>
		<?php endif ?>
		<?php $sames = Yii::app()->session['chooseSame']; ?>
		<?php if (!empty($sames) && is_array($sames)): ?>
		<?php foreach ($sames as $same ): ?>
			<tr >
				<td >
					=
				</td>
				<td >
					<?php echo $same['name'] ?>
				</td>
				<td >
					<?php echo $same['album'] ?>
				</td>
				<td >
					<?php
						$label = '<button class="bt2"> &times; </button>';
						$rurl= array('clearSame','id'=>$same['id']);
						$htmlOption = array();
						echo CHtml::link($label,$rurl,$htmlOption);
					?>
				</td>
			</tr>
		<?php endforeach ?>
		<?php endif ?>
		</tbody>
		<?php if ( !empty($translators) && is_array($translators) || !empty($sames) && is_array($sames) ): ?>
			<tfoot >
				<tr >
					<td  colspan='4'>
						<?php
							$label = '<button class="bt2">Clear All</button>';
							$rurl= array('clearAll');
							$htmlOption = array();
							echo CHtml::link($label,$rurl,$htmlOption);
						?>
						<?php
							$label = '<button class="bt3">Create</button>';
							$rurl= array('createTranslation');
							$htmlOption = array();
							echo CHtml::link($label,$rurl,$htmlOption);
						?>
					</td>
				</tr>
			</tfoot>
		<?php endif ?>
	</table>
</div>
</div>
<div class='clear' style='height:10px'></div>
<div  class="box">
<table class="boxlist">
	<thead >
		<tr >
			<th style='width:40px;' class='sortable' col='track_no'>
				No.
			</th>
			<th >
				Name
			</th>
			<th >
				Album/Artist
			</th>
			<th >
				Link?
			</th>
			<th >
				Action
			</th>
		</tr>
	</thead>
<?php foreach ($result as $row): ?>
	<tr >
		<td >
			<?php echo $row['track_no'] ?>.
		</td>
		<td >
			<?php
				$label = $row['name'].'['.Yii::app()->params['flag']['lang'][$row['lang']].']';
				$rurl= array('admin/quickviewSong','id'=>$row['id']);
				$htmlOption = array('class'=>'colorbox');
				echo CHtml::link($label,$rurl,$htmlOption);
			?>
		</td>
		<td >
			<?php echo $row['album_name'] ?> / 
			<?php echo Fish::xr(Fish::getRow('artist',$row['artist_id']),'name') ?>
		</td>
		<td >
			<?php if (isset($row['belongTo'])&&!empty($row['belongTo'])): ?>
				Original
			<?php elseIf (isset($row['belongTo2'])&&!empty($row['belongTo2'])): ?>
				<?php $tmp = current($row['belongTo2']);
					echo Yii::app()->params['flag']['link_type_id'][$tmp['link_type_id']];
				 ?>
			<?php else: ?>
				-
			<?php endif ?>
		</td>
		<td >
			<?php
			
				# check session chooseOriginal
				if (isset(Yii::app()->session['chooseOriginal'])&&!empty(Yii::app()->session['chooseOriginal'])) {
					# check this is linked
					if ( 
						!(isset($row['belongTo'])) &&
						!(isset($row['belongTo2']))
					) {
						$label = '<button class="bt2">I\'m Translator</button>';
						$rurl= array('chooseTranslator','id'=>$row['id']);
						$htmlOption = array();
						echo CHtml::link($label,$rurl,$htmlOption);
						$label = '<button class="bt2">I\'m Same</button>';
						$rurl= array('chooseSame','id'=>$row['id']);
						$htmlOption = array();
						echo CHtml::link($label,$rurl,$htmlOption);
					}
				} else {
					
					# check this is linked
					if ( 
						(isset($row['belongTo'])&&!empty($row['belongTo'])) ||
						(isset($row['belongTo2'])&&!empty($row['belongTo2']))
					) {
						$label = '<button class="bt2">Edit Me</button>';
						$rurl= array('editLink','id'=>$row['id']);
						$htmlOption = array();
						echo CHtml::link($label,$rurl,$htmlOption);
					} else {
						$label = '<button class="bt2">Use Me</button>';
						$rurl= array('chooseOriginal','id'=>$row['id']);
						$htmlOption = array();
						echo CHtml::link($label,$rurl,$htmlOption);
					}
					
				}
			?>
		</td>
	</tr>
<?php endforeach ?>
</table>
<?php echo $pager ?>
</div>
<script type="text/javascript" charset="utf-8">
$('select#artist_id').change(function(e){
	var target = $( this );
	if (target.val()=='') {
		$('select#album_id option').show();
	}else{
		var flag = true;
		$('select#album_id option').hide();
		$('select#album_id option').each(function(e){
			var option = $( this );
			if ( option.val()=='' || option.attr('artist_id') == target.val()) {
				if (flag) {$('select#album_id').val(option.val());flag=false;};
				option.show();
			}
		});
	}
});
</script>
<style type="text/css" media="screen">
	table.boxlist2 {border:1px solid #CCC;}
	table.boxlist2 td,table.boxlist2 th{padding:5px 6px;}
	table.boxlist2 thead th{background-color:#BF0000; color:#FFF;}
	table.boxlist2 tfoot td{background-color:#003355; color:#FFF;text-align:center;}
</style>